#include "widget.h"
#include "ui_widget.h"



Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置按钮初始文本
    ui->pushButton->setText("开始运行");
    isRunning = false;

    ptr_led = new fsmpLeds(this);
    ptr_bee = new fsmpBeeper(this);
    ptr_vib = new fsmpVibrator(this);
    ptr_bee->setRate(1);
    ptr_sh20 = new fsmpTempHum(this);
    ptr_fan = new fsmpFan(this);  // 初始化风扇控制

    ptr_fan->setSpeed(100);
    ptr_vib->setParameter(0xFFFF, 2000);


    udp = new QUdpSocket(this);
    // 初始化定时器
    timer = new QTimer(this);
    actionTimer = new QTimer(this);

    // 连接信号槽
    connect(udp, &QUdpSocket::readyRead, this, &Widget::readPendingDatagrams);
    connect(timer, &QTimer::timeout, this, &Widget::updateTemperature);
    connect(actionTimer, &QTimer::timeout, this, &Widget::stopLedAndBee);
}

Widget::~Widget()
{
    delete ui;
}

// 开始/停止按钮 - 切换运行状态
void Widget::on_pushButton_clicked()
{
    if (!isRunning) {
        // 开始运行
        isRunning = true;
        ui->pushButton->setText("停止运行");

        // 绑定端口接收数据
        udp->bind(5555, QUdpSocket::ShareAddress);

        // 启动1秒定时器
        timer->start(1000);
        qDebug() << "Started temperature monitoring...";
    } else {
        // 停止运行
        isRunning = false;
        ui->pushButton->setText("开始运行");

        // 停止定时器
        timer->stop();
        actionTimer->stop();

        // 关闭所有设备
        ptr_led->off(fsmpLeds::LED1);
        ptr_led->off(fsmpLeds::LED2);
        ptr_led->off(fsmpLeds::LED3);
        ptr_bee->stop();
        ptr_vib->stop();
        ptr_fan->stop();



        // 解绑UDP端口
        udp->close();

        qDebug() << "Stopped temperature monitoring...";
    }
}

// 定时读取温度并发送
void Widget::updateTemperature()
{
    double temp = ptr_sh20->temperature();
    ui->lcdNumber->display(temp);

    double temp2 = ptr_sh20->humidity();
    ui->lcdNumber_2->display(temp2);

    char send_data[32];
    snprintf(send_data, sizeof(send_data), "devput:%.2lf", temp);

    udp->writeDatagram(send_data, QHostAddress("192.168.54.46"), 6666);
    qDebug() << "Temperature sent:" << temp;
}

// 处理UDP数据接收
void Widget::readPendingDatagrams()
{
    while (udp->hasPendingDatagrams()) {
        QByteArray datagram;
        datagram.resize(udp->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;

        udp->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

        QString data = QString::fromUtf8(datagram).trimmed();
        qDebug() << "Received UDP data:" << data << "from" << sender.toString();

        if (data == "fan-on") {
            // 打开风扇
            ptr_fan->start();

            qDebug() << "Fan turned ON";

            // 开启LED和蜂鸣器
            ptr_led->on(fsmpLeds::LED1);
            ptr_led->on(fsmpLeds::LED2);
            ptr_led->on(fsmpLeds::LED3);
            ptr_bee->start();
            ptr_vib->start();

            // 1秒后关闭LED和蜂鸣器
            actionTimer->start(1000);

        } else if (data == "fan-off") {
            // 关闭风扇
            ptr_fan->stop();

            qDebug() << "Fan turned OFF";

            // 开启LED和蜂鸣器
            ptr_led->on(fsmpLeds::LED1);
            ptr_led->on(fsmpLeds::LED2);
            ptr_led->on(fsmpLeds::LED3);
            ptr_bee->start();
            ptr_vib->start();

            // 1秒后关闭LED和蜂鸣器
            actionTimer->start(1000);
        }
    }
}

// 停止LED和蜂鸣器
void Widget::stopLedAndBee()
{
    ptr_led->off(fsmpLeds::LED1);
    ptr_led->off(fsmpLeds::LED2);
    ptr_led->off(fsmpLeds::LED3);
    ptr_bee->stop();
    ptr_vib->stop();
    actionTimer->stop();
    qDebug() << "LEDs and Beeper turned OFF";
}

// 保留原有按钮功能
void Widget::on_pushButton_2_clicked()
{
    ptr_led->on(fsmpLeds::LED1);
    ptr_led->on(fsmpLeds::LED2);
    ptr_led->on(fsmpLeds::LED3);
    ptr_bee->start();
    ptr_vib->start();
    ptr_fan->start();

    qDebug() << "fan-on";
}

void Widget::on_pushButton_3_clicked()
{
    ptr_led->off(fsmpLeds::LED1);
    ptr_led->off(fsmpLeds::LED2);
    ptr_led->off(fsmpLeds::LED3);
    ptr_bee->stop();
    ptr_vib->stop();
    ptr_fan->stop();

}



